package com.dexilaonian.fragment;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.activity.LoginActivity;
import com.dexilaonian.activity.RecoveryActivity;
import com.dexilaonian.activity.ServiceDetailActivity;
import com.dexilaonian.adpter.ProjectAdapter;
import com.dexilaonian.bean.ProjectBean;
import com.dexilaonian.ui.BaseFragment;
import com.dexilaonian.ui.MyRecyclerViewOnclickInterface;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.JsonUtil;
import com.dexilaonian.utils.Okhttp;
import com.dexilaonian.view.DividerItemDecoration;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

import static com.dexilaonian.R.id.et_sou_text;
import static com.dexilaonian.R.id.tv_search;
public class ProjectFragment extends BaseFragment implements MyRecyclerViewOnclickInterface,View.OnClickListener {
	@Bind(R.id.id_recyclerview)
	RecyclerView idRecyclerview;
	private ProjectAdapter projectAdapter;
	private String project_url = Constant.base_url + "/service/getServiceOfCompanyCategory";
	private List<ProjectBean> list;
	private List<ProjectBean> projectBeen;
	private TextView tvSearch;
	private int  mEt_sou_text;
	private EditText   mEtSou_text;
	private String   mEt_search;
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof RecoveryActivity) {
			RecoveryActivity recoveryActivity = (RecoveryActivity) activity;
			tvSearch = (TextView) recoveryActivity.findViewById(tv_search);
			mEtSou_text = (EditText) recoveryActivity.findViewById(et_sou_text);
			mEt_search = mEtSou_text.getText().toString().trim();
		}
	}

	@Override
	protected void initData(Bundle savedInstanceState) {
	}

	@Override
	protected void initView(View layoutView) {
	}
	@Override
	public int getLayoutID() {
		return R.layout.activity_project;
	}
	@Override
	public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
		View rootView = super.onCreateView(inflater, container, savedInstanceState);
		ButterKnife.bind(this, rootView);
		list = new ArrayList<>();
		tvSearch.setOnClickListener(this);
		mEtSou_text.addTextChangedListener(textWatcher);
		init();
		return rootView;
	}

	private TextWatcher textWatcher = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,int after){
		}
		@Override
		public void afterTextChanged(Editable s) {

			mEt_search = mEtSou_text.getText().toString().trim();
			if (TextUtils.isEmpty(mEt_search)) {
				init();
			}
		}
	};
	private void init() {
		Map<String, String> map = new HashMap<>();
		map.put("city", "深圳市");
		map.put("category", "上门康养");
		map.put("key", mEt_search);
		map.put("index", "1");
		map.put("pageSize", "12");
		Okhttp.postString(false, project_url, map, new Okhttp.CallBac() {
			@Override
			public void onError(Call call, Exception e, String state, int id) {
			}

			@Override
			public void onResponse(String response, int id) {
				try {
					JSONObject jsonObject = new JSONObject(response);
					int        status     = Integer.parseInt(jsonObject.optString("status"));
					if (status == 34 || status == 16) {
						startActivity(new Intent(getActivity(), LoginActivity.class));
						return;
					}
					if (status == 15) {
						showToast("本地服务尚未开通");
					}
					String     data   = jsonObject.optString("data");
					JSONObject object = new JSONObject(data);
					String     items  = object.optString("items");
					projectBeen = JsonUtil.json2beans(items, ProjectBean.class);
					list.clear();
					list.addAll(projectBeen);
					projectAdapter = new ProjectAdapter(getActivity(), list);
					projectAdapter.notifyDataSetChanged();
					idRecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
					idRecyclerview.setAdapter(projectAdapter);
					//添加分割线
					//idRecyclerview.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL_LIST));

					projectAdapter.setItemClickListener(new ProjectAdapter.MyItemClickListener() {
						@Override
						public void onItemClick(View view, int position) {
							//showToast("点击位置"+position);
							Intent intent = new Intent(getActivity(), ServiceDetailActivity.class);
							int    id2    = projectBeen.get(position)
													   .getId();
							Bundle bundle = new Bundle();
							bundle.putInt("IDD", id2);
					/*		intent.putExtra("TIME",times);
							intent.putExtra("level_name",level_name);
							intent.putExtra("address",address);
							intent.putExtra("introduce",introduce);
							intent.putExtra("name",name);
							intent.putExtra("id3",id3);
							intent.putExtra("pic_url",picUrl);*/

							intent.putExtras(bundle);
							startActivity(intent);
						}
					});
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onNoNetwork(String state) {

			}
		});
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		ButterKnife.unbind(this);
	}

	@Override
	public void onItemClick(View view, int position) {
		startActivity(new Intent(getActivity(), ServiceDetailActivity.class));
	}


	@Override
	public void onClick(View view) {
		mEt_search = mEtSou_text.getText().toString().trim();
		init();
	}
}
